package com.liu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.jwt.JwtUtil;
import com.liu.mapper.UmsAdminMapper;
import com.liu.model.dto.LoginDTO;
import com.liu.model.entity.UmsAdmin;
import com.liu.model.vo.ProfileVO;
import com.liu.service.IUmsAdminService;
import com.liu.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liumiao
 * @create 2021-10-29-13:59
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class IUmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin>
        implements IUmsAdminService {
    /*获取登录的用户*/
    @Override
    public UmsAdmin getUserByAdminname(String adminname) {
        return baseMapper.selectOne(new LambdaQueryWrapper<UmsAdmin>().eq(UmsAdmin::getUsername, adminname));
    }

    /*管理员登录*/
    @Override
    public String executeLogin(LoginDTO dto) {
        String token = null;
        try {
            UmsAdmin admin = getUserByAdminname(dto.getUsername());
            String encodePwd = MD5Utils.getPwd(dto.getPassword());
            if(!encodePwd.equals(admin.getPassword()))
            {
                throw new Exception("密码错误");
            }
            token = JwtUtil.generatTokens(String.valueOf(admin.getUsername()));
            System.out.println(token+"刘刘刘");
        } catch (Exception e) {
            log.warn("用户不存在or密码验证失败=======>{}", dto.getUsername());
        }
        return token;
    }

    @Override
    public ProfileVO getUserProfile(String id) {
        return null;
    }
}
